package com.by.wx.mamager;

import com.by.common.conf.WxConfMager;
import com.by.common.cont.WxConst;
import com.by.wx.entity.Button;
import com.by.wx.entity.ClickButton;
import com.by.wx.entity.PhotoOrAlbumButton;
import com.by.wx.entity.SubButton;
import com.by.wx.entity.ViewButton;
import com.by.wx.service.WxService;
import com.by.wx.util.Util;

import net.sf.json.JSONObject;

public class CreateWxCustMenus {

	public static void main(String[] args) {
		WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_ONE);
		Button btn = new Button();
		if (!WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_OPEN_SUB_FIRST)) {
			supperMenus(btn, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_ONE),
					WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_TYPE_ONE),
					WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_TYPE_PARAMS_ONE));
		} else {
			// String supperName =
			// WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_ONE);
			SubButton sbs = new SubButton(
					WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_ONE));
			// String type =
			// WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_ONE);
			// String name =
			// WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_NAME_ONES);
			// String param =
			// WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_PARAMS_ONE);
			subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_ONE),
					WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_NAME_ONES),
					WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_PARAMS_ONE));
			if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_OPEN_SECOND)) {
				subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_TWO),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_NAME_TWOS),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_PARAMS_TWO));
			}
			if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_OPEN_THIRD)) {
				subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_THREE),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_NAME_THREES),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_PARAMS_THREE));
			}
			if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_OPEN_FOURTH)) {
				subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_FOUR),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_NAME_FOURS),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_PARAMS_FOUR));
			}
			if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_OPEN_FIFTH)) {
				subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_FIVE),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_NAME_FIVES),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_ONE_SUB_TYPE_PARAMS_FIVE));
			}
			btn.getButton().add(sbs);
		}
		if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_OPEN_INTERFACE_SUPPER_TWO)) {
			if (!WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_OPEN_SUB_SECOND)) {
				supperMenus(btn, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_TWO),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_TYPE_TWO),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_TYPE_PARAMS_TWO));
			} else {
				SubButton sbs = new SubButton(
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_TWO));
				subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_ONE),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_NAME_ONES),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_PARAMS_ONE));
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_OPEN_SECOND)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_TWO),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_NAME_TWOS),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_PARAMS_TWO));
				}
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_OPEN_THIRD)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_THREE),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_NAME_THREES),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_PARAMS_THREE));
				}
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_OPEN_FOURTH)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_FOUR),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_NAME_FOURS),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_PARAMS_FOUR));
				}
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_OPEN_FIFTH)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_FIVE),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_NAME_FIVES),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_PARAMS_FIVE));
				}
				btn.getButton().add(sbs);
			}
		}
		if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_OPEN_INTERFACE_SUPPER_THREE)) {
			if (!WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_OPEN_SUB_THIRD)) {
				supperMenus(btn,
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_THREE),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_TYPE_THREE),
						WxConfMager
								.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_TYPE_PARAMS_THREE));
			} else {
				SubButton sbs = new SubButton(
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_INTERFACE_SUPPER_NAME_THREE));
				subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_ONE),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_NAME_ONES),
						WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_PARAMS_ONE));
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_OPEN_SECOND)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_TWO),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_NAME_TWOS),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_TWO_SUB_TYPE_PARAMS_TWO));
				}
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_OPEN_THIRD)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_THREE),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_NAME_THREES),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_PARAMS_THREE));
				}
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_OPEN_FOURTH)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_FOUR),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_NAME_FOURS),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_PARAMS_FOUR));
				}
				if (WxConfMager.getBoolean(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_OPEN_FIFTH)) {
					subMenus(sbs, WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_FIVE),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_NAME_FIVES),
							WxConfMager.getProperty(WxConst.WXWEB_OFFICIAL_MENU_DEFINED_THREE_SUB_TYPE_PARAMS_FIVE));
				}
				btn.getButton().add(sbs);
			}
		}
		// 菜单对象
		// Button btn = new Button();
		// 第一个一级菜单
		// btn.getButton().add(new ClickButton("一级点击", "1"));
		// // 第二个一级菜单
		// btn.getButton().add(new ViewButton("一级跳转", "http://www.baidu.com"));
		// // 创建第三个一级菜单
		// SubButton sb = new SubButton("有子菜单");
		// // 为第三个一级菜单增加子菜单
		// sb.getSub_button().add(new PhotoOrAlbumButton("传图", "31"));
		// sb.getSub_button().add(new ClickButton("点击", "32"));
		// sb.getSub_button().add(new ViewButton("网易新闻",
		// "http://news.163.com"));
		// // 加入第三个一级菜单
		// btn.getButton().add(sb);
		// 转为json
		JSONObject jsonObject = JSONObject.fromObject(btn);
		// 准备url
		String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
		url = url.replace("ACCESS_TOKEN", WxService.getAccessToken());
		// 发送请求
		String result = Util.post(url, jsonObject.toString());
		System.out.println(result);
	}

	private static void supperMenus(Button btn, String supperName, String type, String param) {
		if ("click".equals(type)) {
			btn.getButton().add(new ClickButton(supperName, param));
		} else if ("view".equals(type)) {
			btn.getButton().add(new ViewButton(supperName, param));
		} else if ("scancode_push".equals(type)) {

		} else if ("scancode_waitmsg".equals(type)) {

		} else if ("pic_sysphoto".equals(type)) {

		} else if ("pic_photo_or_album".equals(type)) {
			btn.getButton().add(new PhotoOrAlbumButton(supperName, param));
		} else if ("pic_weixin".equals(type)) {

		} else if ("location_select".equals(type)) {

		} else if ("media_id".equals(type)) {

		} else if ("view_limited".equals(type)) {

		}
	}

	private static void subMenus(SubButton sbs, String type, String name, String param) {
		if ("click".equals(type)) {
			sbs.getSub_button().add(new ClickButton(name, param));
		} else if ("view".equals(type)) {
			sbs.getSub_button().add(new ViewButton(name, param));
		} else if ("scancode_push".equals(type)) {

		} else if ("scancode_waitmsg".equals(type)) {

		} else if ("pic_sysphoto".equals(type)) {

		} else if ("pic_photo_or_album".equals(type)) {
			sbs.getSub_button().add(new PhotoOrAlbumButton(name, param));
		} else if ("pic_weixin".equals(type)) {

		} else if ("location_select".equals(type)) {

		} else if ("media_id".equals(type)) {

		} else if ("view_limited".equals(type)) {

		}
	}
}
